﻿/********************************************************
 * 类名： UnitDAL.cs
 * 功能： 计量单位数据层
 *   ver	变更日		作者			变更内容
 * ────────────────────────────────────────
 *   V01.0	2013-11-27	李波			初版
 *
 * Copyright (c) 2013，李波版权所有
 ********************************************************/
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using PSS.Entity;
using PSS.Entity.SetManage.AuxiliaryInfo;
using PSS.Utility;

namespace PSS.DAL.SetManage.AuxiliaryInfo
{
    public class UnitDAL : DbHelperBase
    {
        public int GetTotal()
        {
            var strSql = new StringBuilder();
            strSql.Append(" select  count(*) AS [Total] FROM PSS_Unit ");
            var count = 0;
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            count = Convert.ToInt32(reader["Total"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return count;
            }
        }
        public IList<UnitEntity> GetUnitList(Pagination qcmodel)
        {
            var list = new List<UnitEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            var start = qcmodel.PageSize * qcmodel.PageIndex + 1;
            var end = qcmodel.PageSize + start - 1;
            strSql.Append(" SELECT T.* FROM( ");
            strSql.Append(" SELECT row_number() over (order by CreateTime) AS idx,UnitId,UnitName,CreateTime FROM PSS_Unit ");
            strSql.Append(" WHERE 1=1 ");
            strSql.Append(") AS T ");
            strSql.Append(" Where T.idx between " + start + " and " + end);
            if (qcmodel.SortField != "" && qcmodel.SortOrder != "")
            {
                strSql.Append(" Order by " + qcmodel.SortField + " " + qcmodel.SortOrder);
            }
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new UnitEntity();
                            if (reader["UnitId"] != DBNull.Value)
                                model.UnitId = new Guid(reader["UnitId"].ToString());
                            model.UnitName = reader["UnitName"] == obj ? null : Convert.ToString(reader["UnitName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        public IList<UnitEntity> GetUnitListByCombobox()
        {
            var list = new List<UnitEntity>();
            var strSql = new StringBuilder();
            object obj = DBNull.Value;
            strSql.Append(" SELECT UnitId,UnitName FROM PSS_Unit Order by CreateTime  ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        while (reader.Read())
                        {
                            var model = new UnitEntity();
                            if (reader["UnitId"] != DBNull.Value)
                                model.UnitId = new Guid(reader["UnitId"].ToString());
                            model.UnitName = reader["UnitName"] == obj ? null : Convert.ToString(reader["UnitName"]);
                            list.Add(model);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return list;
        }
        public bool ModifyUnit(UnitEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" UPDATE [PSS_Unit] SET [UnitName] = @UnitName WHERE [UnitId] = @UnitId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@UnitName", model.UnitName);
                    Db.AddInParameter(cmd, "@UnitId", model.UnitId);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        public bool AddUnit(UnitEntity model)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" INSERT INTO [PSS_Unit]([UnitId],[UnitName]) ");
            strSql.Append(" VALUES(@UnitId,@UnitName) ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@UnitId", model.UnitId);
                    Db.AddInParameter(cmd, "@UnitName", model.UnitName);
                    result = Db.ExecuteNonQuery(cmd) > 0;
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
        public UnitEntity GetUnitInfo(string id)
        {
            var model = new UnitEntity();
            object obj = DBNull.Value;
            var strSql = new StringBuilder();
            strSql.Append(" SELECT [UnitId],[UnitName] FROM [PSS_Unit] where [UnitId]=@UnitId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {

                try
                {
                    Db.AddInParameter(cmd, "@UnitId", id);
                    using (IDataReader reader = Db.ExecuteReader(cmd))
                    {
                        if (reader.Read())
                        {
                            if (reader["UnitId"] != DBNull.Value)
                                model.UnitId = new Guid(reader["UnitId"].ToString());
                            model.UnitName = reader["UnitName"] == obj ? null : Convert.ToString(reader["UnitName"]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
                return model;
            }
        }
        public bool RemoveUnit(string id)
        {
            var result = false;
            var strSql = new StringBuilder();
            strSql.Append(" DELETE FROM [PSS_Unit] WHERE UnitId=@UnitId ");
            using (var cmd = Db.GetSqlStringCommond(strSql.ToString()))
            {
                try
                {
                    Db.AddInParameter(cmd, "@UnitId", id);
                    if (Db.ExecuteNonQuery(cmd) > 0)
                    {
                        result = true;
                    }
                }
                catch (Exception ex)
                {
                    EventsLog.WriteLog("ErrorInfo" + ex.Message);
                }
            }
            return result;
        }
    }
}
